package freemind.controller;

import java.util.ArrayList;

import freemind.controller.AtributoEnegModulo;
import freemind.controller.NodeBean;
import freemind.controller.XMLProtocol;
import freemind.main.XMLElement;

public class NodeAdapterBase implements NodeAdapterInterface{

	private NodeBean nodeBean;
	private boolean _isCopied;
	private int nodeCloneId;
	
	private ArrayList<NodeAdapterBase> children;
	
	public NodeAdapterBase(){		
		this( new NodeBean() );
	}
	
	public NodeAdapterBase(NodeBean bean){
		this.nodeBean = bean;
		children = new ArrayList<NodeAdapterBase>();
	}
	
	public void addChild(NodeAdapterBase child){
		this.children.add(child);
	}
	
	public ArrayList<NodeAdapterBase> getChildren(){
		return this.children;
	}		
	
	public void newNode(){
		nodeBean.newNode();
	}
	
	public void toCreate(){
		nodeBean.toCreate();
	}
	
	public boolean isVirtual(){
		return nodeBean.isVirtual();
	}
	
	public boolean isCreated(){
		return nodeBean.isCreated();
	}
	
	public void hasChanged(){
		nodeBean.hasChanged();
		this.setDataAtualizacao(Util.getToday());
	}
	
	public void hasSaved(){
		nodeBean.hasSaved();
	}
	
	public void setAsCopied(int nodeCloneId){
		this._isCopied = true;
		this.nodeCloneId = nodeCloneId;
	}
	
	public void setAsNotCopied(){
		this._isCopied = false;
		this.nodeCloneId = 0;
	}
	
	public int getCloneId(){
		return this.nodeCloneId;
	}
	
	public boolean isCopied(){
		return this._isCopied;
	}
	
	public boolean isACopy(){
		return this.getCopiaId() > 0;
	}
	
	public void setAsNotCopy(){
		this.setCopiaId(0);
	}	
	
	public boolean isChanged(){
		return nodeBean.isChanged();
	}
	
	public void setVirtualId(int virtualId){
		nodeBean.setVirtualId(virtualId);
	}
	
	public int getVirtualId(){
		return nodeBean.getVirtualId();
	}
	
	public void setLinkString(String l){
		nodeBean.setLinkString(l);
	}
	
	public String getLinkString(){
		return nodeBean.getLinkString();
	}
	
	public int getLink() {
		return nodeBean.getLink();
	}

	public void setLink(int link) {
		nodeBean.setLink(link);
	}
			
	public int getId() {
		return nodeBean.getId();
	}

	public int getIdParent() {
		return nodeBean.getIdParent();
	}

	public void setIdParent(int idParent) {
		nodeBean.setIdParent(idParent);
	}

	public int getIdRaiz() {
		return nodeBean.getIdRaiz();
	}

	public void setIdRaiz(int idRaiz) {
		nodeBean.setIdRaiz(idRaiz);
	}
	
	public int getType() {
		return nodeBean.getType();
	}

	public void setType(int type) {
		nodeBean.setType(type);
	}

	public String getText() {
		return nodeBean.getText();
	}

	public void setText(String text) {
		nodeBean.setText(text);		
	}

	public Boolean getLeft() {
		return nodeBean.getLeft();
	}

	public void setLeft(Boolean left) {
		nodeBean.setLeft(left);
	}

	public void setId(int id) {
		nodeBean.setId(id);
	}
	
	public int getCopiaId(){
		return nodeBean.getCopiaId();
	}
	
	public void setCopiaId(int id){
		nodeBean.setCopiaId(id);
	}
	
	public void setAutor(String _autor){
		nodeBean.setAutor( _autor );		
	}
	
	public void setNome(String _nome){
		nodeBean.setNome( _nome );
	}
	
	public String getAutor(){
		return nodeBean.getAutor();
	}
	
	public String getNome(){
		return nodeBean.getNome();
	}	
	
	public void setDataCriacao(String data){
		nodeBean.setDataCriacao(data);		
	}
	
	public void setDataAtualizacao(String data){
		nodeBean.setDataAtualizacao(data);
	}
	
	public String getDataCriacao(){
		return nodeBean.getDataCriacao();
	}
	
	public String getDataAtualizacao(){
		return nodeBean.getDataAtualizacao();
	}
			
	public boolean isRoot(){
		return nodeBean.isRoot();
	}
		
	public void addAtributoExtra(AtributoEnegModulo attr){
		nodeBean.addAtributoExtra(attr);
	}
	
	public ArrayList<AtributoEnegModulo> getAtributos() {
		return nodeBean.getAtributos();
	}	

	public ArrayList<AtributoEnegModulo> getAtributosExtras() {
		return nodeBean.getAtributosExtras();
	}
	
	public void clearAtributosExtras(){
		nodeBean.clearAtributosExtras();
	}
	
	public XMLElement getXMLFromBean(){
		
		XMLElement no = new XMLElement();
		no.setName("no");
		
		XMLElement meta = new XMLElement();
		meta.setName("meta");				
	
		for(AtributoEnegModulo attr: nodeBean.getAtributos()){
			XMLElement attrElement = new XMLElement();
			attrElement.setName(attr.getNome());
			attrElement.setContent(attr.getValor());
			
			meta.addChild(attrElement);
		}
		
		XMLElement extras = new XMLElement();
		extras.setName("extras");
		
			for(AtributoEnegModulo attr: nodeBean.getAtributosExtras()){
				XMLElement attrElement = new XMLElement();
				attrElement.setName(attr.getNome());
				attrElement.setContent(attr.getValor());
				
				extras.addChild(attrElement);
			}
		
		no.addChild(meta);
		no.addChild(extras);		
		
		return no;
	}	
	
	public void getBeanFromXML(XMLElement noElement){
		XMLElement metaElement = (XMLElement) noElement.getChildren().get(0);		
	
		XMLElement virtualId = (XMLElement) metaElement.getChildren().get(0);
		XMLElement id = (XMLElement) metaElement.getChildren().get(1);
		XMLElement idParent = (XMLElement) metaElement.getChildren().get(2);
		XMLElement idRaiz = (XMLElement) metaElement.getChildren().get(3);
		XMLElement isLeft = (XMLElement) metaElement.getChildren().get(4);
		XMLElement tipo = (XMLElement) metaElement.getChildren().get(5);
		XMLElement texto = (XMLElement) metaElement.getChildren().get(6);		
		XMLElement link = (XMLElement) metaElement.getChildren().get(7);
		XMLElement copiaId = (XMLElement) metaElement.getChildren().get(8);
			
		NodeBean bean = new NodeBean(
				Integer.parseInt( virtualId.getContent() ),
				Integer.parseInt( id.getContent() ),
				Integer.parseInt( idParent.getContent() ),
				Integer.parseInt( idRaiz.getContent() ),
				Boolean.parseBoolean( isLeft.getContent() ),
				Integer.parseInt( tipo.getContent() ),
				texto.getContent(),
				Integer.parseInt( link.getContent() ),
				Integer.parseInt( copiaId.getContent() )
				);		
		
		this.nodeBean = bean;		
	}		
	
	public static NodeAdapterBase getAdapterFromEnegModulo(int eneg_modulo){
		return NodeAdapterBase.getAdapterFromEnegModulo(eneg_modulo,new NodeBean());
	}
	
	public static NodeAdapterBase getAdapterFromEnegModulo(int eneg_modulo,NodeBean bean){
		if( eneg_modulo == XMLProtocol.DIALOGO ){
			return new NodeAdapterDialogos(bean);
		}
		else if( eneg_modulo == XMLProtocol.RISCOS ){
			return new NodeAdapterRiscos(bean);
		}
		
		return null;
		
	}
	
	public NodeBean getBean(){
		return this.nodeBean;
	}

	public void setMetaDados(XMLElement meta) {
		XMLElement nome = (XMLElement) meta.getChildren().get(0);
		XMLElement autor = (XMLElement) meta.getChildren().get(1);
		XMLElement dataCriacao = (XMLElement) meta.getChildren().get(2);
		XMLElement dataAtualizacao = (XMLElement) meta.getChildren().get(3);
	
		this.getBean().setNome(nome.getContent());
		this.getBean().setAutor(autor.getContent());
		this.getBean().setDataCriacao(dataCriacao.getContent());
		this.getBean().setDataAtualizacao(dataAtualizacao.getContent());		
	}
	
	public XMLElement createMapMetaXML(){
		XMLElement metaDados = new XMLElement();
		metaDados.setName("meta");
		
		XMLElement nome = new XMLElement();
		nome.setName("nome");
		nome.setContent( this.getNome() );
		
		XMLElement autor = new XMLElement();
		autor.setName("autor");
		autor.setContent( this.getAutor() );
		
		XMLElement dataCriacao = new XMLElement();
		dataCriacao.setName("data_criacao");
		dataCriacao.setContent( this.getDataCriacao() );
		
		XMLElement dataAtualizacao = new XMLElement();
		dataAtualizacao.setName("data_atualizacao");
		dataAtualizacao.setContent( this.getDataAtualizacao() );
		
		metaDados.addChild(nome);
		metaDados.addChild(autor);
		metaDados.addChild(dataCriacao);
		metaDados.addChild(dataAtualizacao);	
		
		return metaDados;
	}
	
}
